home *** CD-ROM | disk | FTP | other *** search
/ World of Education / World of Education.iso / world_z / zphere.zip / ZPHERE.CPP < prev    next >
C/C++ Source or Header  |  1993-01-06  |  8KB  |  383 lines

  1. /**
  2. *        Zphere - Zapping sphere is a mostly harmless visual display
  3. *            of a simplifiled Van de Graf type of simulation.
  4. *
  5. *        by Gil Nardo @Migrant Computing Services, Jan 1992
  6. **/
  7. #include <fstream.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <dos.h>    // for sleep()
  11. #include <time.h> // for randomize()
  12. #include <string.h> // for strcpy()
  13. #include <graphics.h>
  14. #include <conio.h>
  15.  
  16. #define BGIDIRECTORY "c:\\borlandc\\bgi"
  17.  
  18. int nowpen = 2;
  19. int max_x, max_y, mid_y, mid_x, min_x, min_y, size_x, size_y;
  20.  
  21. void
  22. setlimits()
  23. {
  24.     min_x = 0;
  25.     min_y = 0;
  26.     max_x = getmaxx();
  27.     max_y = getmaxy();
  28.     size_x = max_x - min_x + 1;
  29.     size_y = max_y - min_y + 1;
  30.     mid_y = size_y / 2;
  31.     mid_x = size_x / 2;
  32. }
  33.  
  34.  
  35. void
  36. open_scene()
  37. {
  38.     // int gdriver = DETECT, gmode = VGAHI, errorcode;
  39.     int gdriver, gmode, errorcode;
  40.     char usebgidir[80];
  41.     int maxtry;
  42.  
  43.  
  44.     // use this section if you have access to egavga.obj
  45.     // otherwise comment next 6 lines
  46.     errorcode = registerbgidriver(EGAVGA_driver);
  47.     if (registerbgidriver(EGAVGA_driver) < 0)
  48.     {
  49.         cerr << "Graphics error: " << grapherrormsg(errorcode) << '\n';
  50.         exit(1);
  51.     }
  52.  
  53.     *usebgidir = NULL;
  54.     for(maxtry = 0; ; maxtry++)
  55.     {
  56.         gdriver = DETECT;
  57.         gmode = VGAHI;
  58.         initgraph(&gdriver, &gmode, usebgidir);
  59.         errorcode = graphresult();
  60.         if (errorcode == grOk)
  61.             break;
  62.         if (maxtry == 0)
  63.         {
  64.             strcpy(usebgidir, BGIDIRECTORY);
  65.             continue;
  66.         }
  67.         if (maxtry > 3)
  68.         {
  69.             cerr << "Graphics Error: " << grapherrormsg(errorcode) << ".\n";
  70.             exit(1);
  71.         }
  72.         if (errorcode == grFileNotFound)
  73.         {
  74.             cout << "Requires Borland Graphics Interface (BGI) modules\n";
  75.             cout << "Please enter the BGI path: ";
  76.             cin >> usebgidir;
  77.         }
  78.         else
  79.         {
  80.             cerr << "Graphics Error: " << grapherrormsg(errorcode) << ".\n";
  81.             exit(1);
  82.         }
  83.     }
  84.     setlimits();
  85.     return;
  86. }
  87.  
  88.  
  89. void
  90. close_scene()
  91. {
  92.     closegraph();
  93.     cout << "Zphere = Zapping Sphere\n";
  94.     return;
  95. }
  96.  
  97.  
  98. void
  99. plotpen(int whichcolor)
  100. {
  101.     nowpen = whichcolor;
  102.     setcolor(whichcolor);
  103.     return;
  104. }
  105.  
  106.  
  107. main()
  108. {
  109.     register long index;
  110.     int xscan, yscan;
  111.     int numpoints;
  112.     int lastx;
  113.     int useradius;
  114.     int usersquared;
  115.     int addx, addy, usex, usey, signx, signy;
  116.     int quartered;
  117.     int xwander, ywander;
  118.     struct pointtype *thunder;
  119.  
  120.  
  121.     open_scene();
  122.  
  123.     randomize();
  124.  
  125.     useradius = (size_x < size_y) ? size_x : size_y;
  126.     useradius /= 4;
  127.     usersquared = useradius * useradius;
  128.     quartered = useradius * 3 / 4; 
  129.     xwander = (size_x / 2  - useradius) / 3; 
  130.     ywander = (size_y / 2  - useradius) / 3; 
  131.  
  132.     for(index = 0; index < 20000; index++)
  133.     {
  134.         plotpen(random(MAXCOLORS - 1));
  135.         putpixel(random(max_x), random(max_y), nowpen);
  136.     }
  137.  
  138.     thunder = new(struct pointtype [size_y * 4]);
  139.  
  140.     plotpen(BLUE);
  141.     outtextxy(mid_x - mid_x / 4, mid_y, "Zphere by Gil Nardo");
  142.     plotpen(YELLOW);
  143.     outtextxy(mid_x - mid_x / 4 - 2, mid_y + 1, "Zphere by Gil Nardo");
  144.  
  145.     for(;;)
  146.     {
  147.         signx = (random(2) == 1) ? 1 : -1;
  148.         signy = (random(2) == 1) ? 1 : -1;
  149.         usex = random(useradius + 20);
  150.         usex = (usex >= useradius) ? useradius : usex;
  151.         usey = sqrt(usersquared - usex * usex);
  152.  
  153.         numpoints = 0;
  154.         thunder[numpoints].x = mid_x + signx * usex;
  155.         thunder[numpoints].y = mid_y - signy * usey;
  156.         usex = thunder[numpoints].x;
  157.         usey = thunder[numpoints].y;
  158.         numpoints++;
  159.  
  160.         if (signx < 0)
  161.         {
  162.             if (signy < 0)
  163.             { // lower left
  164.                 if (usex < mid_x - quartered)
  165.                 { // zap horizontal
  166.                     addy = usey;
  167.                     xscan = usex;
  168.                     for(; xscan >= 0; xscan -= 8)
  169.                     {
  170.                         if (random(2) == 1)
  171.                         {
  172.                             if (random(2) == 1)
  173.                                 addy -= random(ywander);
  174.                             else
  175.                                 addy += random(ywander);
  176.                             if (addy < usey)
  177.                                 addy = usey;
  178.                             if (addy > max_y)
  179.                                 addy = max_y;
  180.                             thunder[numpoints].x = xscan;
  181.                             thunder[numpoints].y = addy;
  182.                             numpoints++;
  183.                         }
  184.                     }
  185.                 }
  186.                 else
  187.                 { // zap vertical
  188.                     addx = usex;
  189.                     yscan = usey;
  190.                     for(; yscan <= max_y; yscan += 8)
  191.                     {
  192.                         if (random(2) == 1)
  193.                         {
  194.                             if (random(2) == 1)
  195.                                 addx -= random(xwander);
  196.                             else
  197.                                 addx += random(xwander);
  198.                             if (addx > usex)
  199.                                 addx = usex;
  200.                             if (addx < 0)
  201.                                 addx = 0;
  202.                             thunder[numpoints].x = addx;
  203.                             thunder[numpoints].y = yscan;
  204.                             numpoints++;
  205.                         }
  206.                     }
  207.                 }
  208.             }
  209.             else
  210.             { // upper left
  211.                 if (usex < mid_x - quartered)
  212.                 { // zap horizontal
  213.                     addy = usey;
  214.                     xscan= usex;
  215.                     for(; xscan >= 0; xscan -= 8)
  216.                     {
  217.                         if (random(2) == 1)
  218.                         {
  219.                             if (random(2) == 1)
  220.                                 addy -= random(ywander);
  221.                             else
  222.                                 addy += random(ywander);
  223.                             if (addy > usey)
  224.                                 addy = usey;
  225.                             if (addy < 0)
  226.                                 addy = 0;
  227.                             thunder[numpoints].x = xscan;
  228.                             thunder[numpoints].y = addy;
  229.                             numpoints++;
  230.                         }
  231.                     }
  232.                 }
  233.                 else
  234.                 { // zap vertical
  235.                     addx = usex;
  236.                     yscan = usey;
  237.                     for(; yscan >= 0; yscan -=8)
  238.                     {
  239.                         if (random(2) == 1)
  240.                         {
  241.                             if (random(2) == 1)
  242.                                 addx -= random(xwander);
  243.                             else
  244.                                 addx += random(xwander);
  245.                             if (addx > usex)
  246.                                 addx = usex;
  247.                             if (addx < 0)
  248.                                 addx = 0;
  249.                             thunder[numpoints].x = addx;
  250.                             thunder[numpoints].y = yscan;
  251.                             numpoints++;
  252.                         }
  253.                     }
  254.                 }
  255.             }
  256.         }
  257.         else // assume signx >= 0
  258.         {
  259.             if (signy < 0)
  260.             { // lower right
  261.                 if (usex < mid_x + quartered)
  262.                 { // zap vertical
  263.                     addx = usex;
  264.                     yscan = usey;
  265.                     for(; yscan <= max_y; yscan += 8)
  266.                     {
  267.                         if (random(2) == 1)
  268.                         {
  269.                             if (random(2) == 1)
  270.                                 addx -= random(xwander);
  271.                             else
  272.                                 addx += random(xwander);
  273.                             if (addx < usex)
  274.                                 addx = usex;
  275.                             if (addx > max_x)
  276.                                 addx = max_x;
  277.                             thunder[numpoints].x = addx;
  278.                             thunder[numpoints].y = yscan;
  279.                             numpoints++;
  280.                         }
  281.                     }
  282.                 }
  283.                 else
  284.                 { // zap horizontal
  285.                     addy = usey;
  286.                     xscan= usex;
  287.                     for(; xscan <= max_x; xscan += 8)
  288.                     {
  289.                         if (random(2) == 1)
  290.                         {
  291.                             if (random(2) == 1)
  292.                                 addy -= random(ywander);
  293.                             else
  294.                                 addy += random(ywander);
  295.                             if (addy < usey)
  296.                                 addy = usey;
  297.                             if (addy > max_y)
  298.                                 addy = max_y;
  299.                             thunder[numpoints].x = xscan;
  300.                             thunder[numpoints].y = addy;
  301.                             numpoints++;
  302.                         }
  303.                     }
  304.                 }
  305.             }
  306.             else
  307.             { // upper right
  308.                 if (usex < mid_x + quartered)
  309.                 { // zap vertical
  310.                     addx = usex;
  311.                     yscan = usey;
  312.                     for(; yscan >= 0; yscan -= 8)
  313.                     {
  314.                         if (random(2) == 1)
  315.                         {
  316.                             if (random(2) == 1)
  317.                                 addx -= random(xwander);
  318.                             else
  319.                                 addx += random(xwander);
  320.                             if (addx < usex)
  321.                                 addx = usex;
  322.                             if (addx > max_x)
  323.                                 addx = max_x;
  324.                             thunder[numpoints].x = addx;
  325.                             thunder[numpoints].y = yscan;
  326.                             numpoints++;
  327.                         }
  328.                     }
  329.                 }
  330.                 else
  331.                 { // zap horizontal
  332.                     addy = usey;
  333.                     xscan= usex;
  334.                     for(; xscan <= max_x; xscan += 8)
  335.                     {
  336.                         if (random(2) == 1)
  337.                         {
  338.                             if (random(2) == 1)
  339.                                 addy -= random(ywander);
  340.                             else
  341.                                 addy += random(ywander);
  342.                             if (addy > usey)
  343.                                 addy = usey;
  344.                             if (addy < 0)
  345.                                 addy = 0;
  346.                             thunder[numpoints].x = xscan;
  347.                             thunder[numpoints].y = addy;
  348.                             numpoints++;
  349.                         }
  350.                     }
  351.                 }
  352.             }
  353.         }
  354.  
  355.         plotpen(WHITE);
  356.         drawpoly(numpoints, (int *)thunder);
  357.  
  358.         if (kbhit())
  359.             break;
  360.         delay(25);
  361.  
  362.         plotpen(RED);
  363.         drawpoly(numpoints, (int *)thunder);
  364.  
  365.         delay(25);
  366.  
  367.         plotpen(BLACK);
  368.         drawpoly(numpoints, (int *)thunder);
  369.  
  370.         if (numpoints % 2)
  371.         { // allow births
  372.             plotpen(1 + random(MAXCOLORS - 2)); // ignore blackout
  373.             putpixel(random(max_x), random(max_y), nowpen);
  374.         }
  375.     }
  376.  
  377.     close_scene();
  378.  
  379.     delete(thunder);
  380.  
  381.     return(0);
  382. }
  383.